package com.ninth_group.po;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class ProvinceWeather {
    private String city;
    private String temp;
    private LocalTime sunriseTime;
    private LocalTime sunsetTime;
    private String windDirection; // wind direction--风向
    private String uv; // ultraviolet ray--紫外线
    private String humidity;
    private int pm25;
    private String aq; // air quality--空气质量
    private String currentWeather;
    private String weather;
    private int weatherCode;
    // 静态映射方法
    public static ProvinceWeather mapRow(ResultSet rs) throws SQLException {
        ProvinceWeather pw = new ProvinceWeather();
        pw.setCity(rs.getString("city"));
        pw.setTemp(rs.getString("temp"));
        pw.setSunriseTime(parseToLocalTime(rs.getString("sunrise_time")));
        pw.setSunsetTime(parseToLocalTime(rs.getString("sunset_time")));
        pw.setWindDirection(rs.getString("wind_direction"));
        pw.setUv(rs.getString("uv"));
        pw.setHumidity(rs.getString("humidity"));
        pw.setPm25(rs.getInt("pm25"));
        pw.setAq(rs.getString("aq"));
        pw.setCurrentWeather(rs.getString("current_weather"));
        pw.setWeather(rs.getString("weather"));
        return pw;
    }

    // 将 mm:ss 或 H:mm 格式字符串转成 LocalTime
    private static LocalTime parseToLocalTime(String timeStr) {
        if (timeStr == null || timeStr.isBlank()) {
            return null;
        }
        try {
            // 使用格式 "H:mm" 可兼容 "8:00"、"13:18"
            return LocalTime.parse(timeStr, DateTimeFormatter.ofPattern("H:mm"));
        } catch (DateTimeParseException e) {
            throw new IllegalArgumentException("Invalid time format: " + timeStr, e);
        }
    }

    public ProvinceWeather() {
    }

    public ProvinceWeather(String city, String temp, LocalTime sunriseTime,
                           LocalTime sunsetTime, String windDirection,
                           String uv, String humidity, int pm25, String aq,
                           String currentWeather, String weather, int weatherCode) {
        this.city = city;
        this.temp = temp;
        this.sunriseTime = sunriseTime;
        this.sunsetTime = sunsetTime;
        this.windDirection = windDirection;
        this.uv = uv;
        this.humidity = humidity;
        this.pm25 = pm25;
        this.aq = aq;
        this.currentWeather = currentWeather;
        this.weather = weather;
        this.weatherCode = weatherCode;
    }

    @Override
    public String toString() {
        return "ProvinceWeather{" +
                "city='" + city + '\'' +
                ", temp='" + temp + '\'' +
                ", sunriseTime=" + sunriseTime +
                ", sunsetTime=" + sunsetTime +
                ", windDirection='" + windDirection + '\'' +
                ", uv='" + uv + '\'' +
                ", humidity='" + humidity + '\'' +
                ", pm25=" + pm25 +
                ", aq='" + aq + '\'' +
                ", currentWeather='" + currentWeather + '\'' +
                ", weather='" + weather + '\'' +
                ", weatherCode=" + weatherCode +
                '}';
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getTemp() {
        return temp;
    }
    public void setTemp(String temp) {
        this.temp = temp;
    }
    public LocalTime getSunriseTime() {
        return sunriseTime;
    }
    public void setSunriseTime(LocalTime sunriseTime) {
        this.sunriseTime = sunriseTime;
    }
    public LocalTime getSunsetTime() {
        return sunsetTime;
    }
    public void setSunsetTime(LocalTime sunsetTime) {
        this.sunsetTime = sunsetTime;
    }
    public String getWindDirection() {
        return windDirection;
    }
    public void setWindDirection(String windDirection) {
        this.windDirection = windDirection;
    }
    public String getUv() {
        return uv;
    }
    public void setUv(String uv) {
        this.uv = uv;
    }
    public String getHumidity() {
        return humidity;
    }
    public void setHumidity(String humidity) {
        this.humidity = humidity;
    }
    public int getPm25() {
        return pm25;
    }
    public void setPm25(int pm25) {
        this.pm25 = pm25;
    }
    public String getAq() {
        return aq;
    }
    public void setAq(String aq) {
        this.aq = aq;
    }
    public String getCurrentWeather() {
        return currentWeather;
    }
    public void setCurrentWeather(String currentWeather) {
        this.currentWeather = currentWeather;
    }
    public String getWeather() {
        return weather;
    }
    public void setWeather(String weather) {
        this.weather = weather;
    }

    public int getWeatherCode() {
        return weatherCode;
    }

    public void setWeatherCode(int weatherCode) {
        this.weatherCode = weatherCode;
    }
}
